package com.synjones.handsetS8.ild.adapter.viewholder;

import android.os.Handler;

import com.synjones.handsetS8.Utils.util;
import com.synjones.m1card.M1CardInfo;
import com.synjones.reader.IDCardReaderModule;


/**
 * @author Chris Chan
 * Create on: 2021/7/23}16:54
 * For: 寻找手环的线程 花名册页面
 * Explain:
 */
public class FindBraceletRunnable implements Runnable {
    private boolean findingCard = true;
    private long timeout = 5000;
    private Handler handler;
    private IDCardReaderModule idCardReaderModule;
    private int code;

    public FindBraceletRunnable() {
    }

    public FindBraceletRunnable(boolean findingCard, long timeout, Handler handler, IDCardReaderModule idCardReaderModule, int code) {
        this.findingCard = findingCard;
        this.timeout = timeout;
        this.handler = handler;
        this.idCardReaderModule = idCardReaderModule;
        this.code = code;
    }

    public boolean isFindingCard() {
        return findingCard;
    }

    public void setFindingCard(boolean findingCard) {
        this.findingCard = findingCard;
    }

    public long getTimeout() {
        return timeout;
    }

    public void setTimeout(long timeout) {
        this.timeout = timeout;
    }

    public Handler getHandler() {
        return handler;
    }

    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    public IDCardReaderModule getIdCardReaderModule() {
        return idCardReaderModule;
    }

    public void setIdCardReaderModule(IDCardReaderModule idCardReaderModule) {
        this.idCardReaderModule = idCardReaderModule;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public void run() {
        M1CardInfo m1CardInfo = null;
        long t = System.currentTimeMillis();
        while (findingCard && System.currentTimeMillis() - t < timeout && m1CardInfo == null) {
            m1CardInfo = idCardReaderModule.findM1Card();
            if (m1CardInfo != null) {
                handler.obtainMessage(code, m1CardInfo).sendToTarget();
                break;
            }
            util.sleep(50);
        }
        findingCard = false;
        if (m1CardInfo == null)
            handler.obtainMessage(code, null).sendToTarget();
    }
}
